package com.egg.auth.enums.role;


import cn.hutool.core.util.ObjectUtil;
import com.egg.common.swagger.annotation.SwaggerDisplayAnnotation;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;

/**
 * 角色数据权限枚举类
 *
 * @author east
 */
@Getter
@SwaggerDisplayAnnotation
@AllArgsConstructor
public enum RoleDataScopeEnum {


    ALL("所有","all"),
    DEPT_AND_LOWER_DEPT("本部门以及下级部门","dept-lowerDept"),
    DEPT("本部门","dept"),
    CUSTOM("自定义","custom"),
    ONESELF("仅本人数据权限","oneself"),
    ;


    private String message;
    private String code;


    public static boolean isInvalid(String code) {
        if (null == code) {
            return true;
        }
        return !Arrays.stream(values()).anyMatch(o -> Objects.equals(code, o.getCode()));
    }

    public static boolean isAll(Collection<String> dataScope) {
        if (ObjectUtil.isEmpty(dataScope)) {
            return false;
        }
        return dataScope.stream().anyMatch(o -> Objects.equals(o, ALL.getCode()));
    }

    public static boolean isDeptAndChild(Collection<String> dataScope) {
        if (ObjectUtil.isEmpty(dataScope)) {
            return false;
        }
        return dataScope.stream().anyMatch(o -> Objects.equals(o, DEPT_AND_LOWER_DEPT.getCode()));
    }

    public static boolean isDept(Collection<String> dataScope) {
        if (ObjectUtil.isEmpty(dataScope)) {
            return false;
        }
        return dataScope.stream().anyMatch(o -> Objects.equals(o, DEPT.getCode()));
    }

    public static boolean isCustom(Collection<String> dataScope) {
        if (ObjectUtil.isEmpty(dataScope)) {
            return false;
        }
        return dataScope.stream().anyMatch(o -> Objects.equals(o, CUSTOM.getCode()));
    }

    public static boolean isOneself(Collection<String> dataScope) {
        if (ObjectUtil.isEmpty(dataScope)) {
            return false;
        }
        return dataScope.stream().anyMatch(o -> Objects.equals(o, ONESELF.getCode()));
    }

    @Override
    public String toString() {
        return message + ":" + code;
    }
}
